package com.amumu.drama.common.modules.category.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.amumu.drama.common.base.api.CommonPage;
import com.amumu.drama.common.base.api.CommonResult;
import com.amumu.drama.common.modules.category.dto.GameDto;
import com.amumu.drama.common.modules.category.dto.GameQueryDto;
import com.amumu.drama.common.modules.category.mapping.GameSkuVoMapping;
import com.amumu.drama.common.modules.category.mapping.GameTemplateInfoVoMapping;
import com.amumu.drama.common.modules.category.mapping.GameVoMapping;
import com.amumu.drama.common.modules.category.service.GameService;
import com.amumu.drama.common.modules.category.vo.GamePortalVo;
import com.amumu.drama.common.modules.category.vo.GameVo;
import com.amumu.drama.mapper.PmsProductCategoryMapper;
import com.amumu.drama.mapper.PmsProductTemplateMapper;
import com.amumu.drama.mapper.PmsSkuStockMapper;
import com.amumu.drama.model.PmsProductCategory;
import com.amumu.drama.model.PmsProductTemplate;
import com.amumu.drama.model.PmsSkuStock;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @Auther: DM
 * @Date: 2024/6/27 15:18
 * @Description:
 */
@Service
@Log4j2
public class PmsProductCategoryServiceImpl extends ServiceImpl<PmsProductCategoryMapper, PmsProductCategory> implements GameService {
    @Autowired
    PmsProductCategoryMapper pmsProductCategoryMapper;

    @Autowired
    PmsProductTemplateMapper pmsProductTemplateMapper;

    @Autowired
    PmsSkuStockMapper pmsSkuStockMapper;


    @Override
    public CommonResult<String> addOrUpdateGame(GameDto anchorDto) {
        try {
            if (StringUtils.isBlank(anchorDto.getFirstLetter())) {
                return CommonResult.failed("首字母不能为空");
            }
            anchorDto.setFirstLetter(anchorDto.getFirstLetter().toUpperCase());
            if (Objects.isNull(anchorDto.getGameId())) {
                QueryWrapper<PmsProductCategory> pmsProductCategoryQueryWrapper = new QueryWrapper<>();
                pmsProductCategoryQueryWrapper.eq("name", anchorDto.getName());
                pmsProductCategoryQueryWrapper.eq("delete_status", 0);
                PmsProductCategory pmsProductCategory = pmsProductCategoryMapper.selectOne(pmsProductCategoryQueryWrapper);
                if (Objects.nonNull(pmsProductCategory)) {
                    return CommonResult.failed("游戏名称重复");
                }
                PmsProductCategory game = new PmsProductCategory();
                BeanUtil.copyProperties(anchorDto, game);
                pmsProductCategoryMapper.insert(game);
                return CommonResult.success(game.getName());
            }
            PmsProductCategory pmsProductCategory = pmsProductCategoryMapper.selectById(anchorDto.getGameId());
            if (Objects.isNull(pmsProductCategory)) {
                return CommonResult.failed("游戏不存在");
            }
            if (!Objects.equals(pmsProductCategory.getName(), anchorDto.getName())) {
                QueryWrapper<PmsProductCategory> pmsProductCategoryQueryWrapper = new QueryWrapper<>();
                pmsProductCategoryQueryWrapper.eq("name", anchorDto.getName());
                pmsProductCategoryQueryWrapper.eq("delete_status", 0);
                PmsProductCategory pmsProductCategory1 = pmsProductCategoryMapper.selectOne(pmsProductCategoryQueryWrapper);
                if (Objects.nonNull(pmsProductCategory1)) {
                    return CommonResult.failed("游戏名称重复");
                }
            }
            BeanUtil.copyProperties(anchorDto, pmsProductCategory);
            if (pmsProductCategoryMapper.updateById(pmsProductCategory) < 1) {
                return CommonResult.failed("游戏修改失败");
            }
            return CommonResult.success(pmsProductCategory.getName());
        } catch (Exception e) {
            log.error("游戏创建或者修改失败", e);
            return CommonResult.failed("游戏创建或者修改失败 " + e.getMessage());
        }
    }

    @Override
    public CommonResult<String> deleteGame(Long gameId) {
        UpdateWrapper<PmsProductCategory> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", gameId).set("delete_status", 1);
        if (!update(updateWrapper)) {
            return CommonResult.failed("删除游戏失败");
        }
        return CommonResult.success();
    }

    @Override
    public CommonResult<String> editSort(Long gameId, Integer sort) {
        UpdateWrapper<PmsProductCategory> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", gameId).set("sort", sort);
        if (!update(updateWrapper)) {
            return CommonResult.failed("修改排序失败");
        }
        ;
        return CommonResult.success();
    }

    @Override
    public CommonResult<String> batchWithexecutionStrategy(List<Long> gameIds, Integer performOperations, Integer executionStrategy) {
        if (Objects.equals(0, executionStrategy)) {
            UpdateWrapper<PmsProductCategory> updateWrapper = new UpdateWrapper<>();
            updateWrapper.in("id", gameIds).set("show_status", performOperations);
            update(updateWrapper);
            if (!update(updateWrapper)) {
                return CommonResult.failed("批量操作显示与否失败");
            }
            return CommonResult.success();
        } else if (Objects.equals(1, executionStrategy)) {
            UpdateWrapper<PmsProductCategory> updateWrapper = new UpdateWrapper<>();
            updateWrapper.in("id", gameIds).set("nav_status", performOperations);
            update(updateWrapper);
            if (!update(updateWrapper)) {
                return CommonResult.failed("批量操作推荐与否失败");
            }
            return CommonResult.success();
        }
        return CommonResult.failed("执行的策略传入有问题");
    }

    @Override
    public CommonPage<GameVo> pageGameList(GameQueryDto gameQueryDto) {
        // 创建分页对象
        PageHelper.startPage(gameQueryDto.getPageNum(), gameQueryDto.getPageSize());
        // 创建查询条件
        QueryWrapper<PmsProductCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(gameQueryDto.getName()), "name", gameQueryDto.getName());
        queryWrapper.eq(Objects.nonNull(gameQueryDto.getNavStatus()), "nav_status", gameQueryDto.getNavStatus());
        queryWrapper.eq(Objects.nonNull(gameQueryDto.getShowStatus()), "show_status", gameQueryDto.getShowStatus());
        queryWrapper.eq("delete_status", 0);
        queryWrapper.orderByAsc("first_letter");
        // 执行分页查询
        List<PmsProductCategory> pmsProductCategories = pmsProductCategoryMapper.selectList(queryWrapper);
        return CommonPage.restPageAndConvert(pmsProductCategories, GameVoMapping.INSTANCE::convertTo);
    }

    @Override
    public CommonResult<GameVo> getGameInfo(Long gameId) {
        GameVo gameVo = new GameVo();
        PmsProductCategory productCategory = pmsProductCategoryMapper.selectById(gameId);
        if (Objects.isNull(productCategory)) {
            return CommonResult.failed("游戏模板不存在");
        }
        BeanUtil.copyProperties(productCategory, gameVo);
        return CommonResult.success(gameVo);
    }

    @Override
    public CommonResult<GamePortalVo> getGameAndSkuInfo(Long gameId) {
        if (Objects.isNull(gameId)) {
            return CommonResult.failed("游戏ID不能为空");
        }
        GamePortalVo gamePortalVo = new GamePortalVo();
        PmsProductCategory pmsProductCategory = pmsProductCategoryMapper.selectById(gameId);
        if (Objects.isNull(pmsProductCategory)) {
            return CommonResult.failed("游戏不存在");
        }
        gamePortalVo.setGame(GameVoMapping.INSTANCE.convertTo(pmsProductCategory));
        QueryWrapper<PmsProductTemplate> pmsProductCategoryQueryWrapper = new QueryWrapper<>();
        pmsProductCategoryQueryWrapper.eq("product_category_id", gameId);
        pmsProductCategoryQueryWrapper.eq("delete_status", 0);
        pmsProductCategoryQueryWrapper.last("limit 1");
        PmsProductTemplate pmsProductTemplate = pmsProductTemplateMapper.selectOne(pmsProductCategoryQueryWrapper);
        gamePortalVo.setGameTemplateInfoVo(GameTemplateInfoVoMapping.INSTANCE.convertTo(pmsProductTemplate));
        QueryWrapper<PmsSkuStock> pmsSkuStockQueryWrapper = new QueryWrapper<>();
        pmsSkuStockQueryWrapper.eq("product_category_id", gameId);
        pmsSkuStockQueryWrapper.eq("delete_status", 0);
        pmsSkuStockQueryWrapper.eq("publish_status", 1);
        pmsSkuStockQueryWrapper.orderByDesc("sort").orderByAsc("price");
        List<PmsSkuStock> pmsSkuStocks = pmsSkuStockMapper.selectList(pmsSkuStockQueryWrapper);
        gamePortalVo.setGameSkuVos(GameSkuVoMapping.INSTANCE.convertTo(pmsSkuStocks));
        return CommonResult.success(gamePortalVo);
    }
}
